package com.sky.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "菜品分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /*
    * 菜品分类分页查询
    * */
    @ApiOperation(value = "菜品分类分页查询")
    @GetMapping("/page")
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("菜品分类分页查询:{}",categoryPageQueryDTO);
        Page<Category> pageInfo = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageInfo);
    }
    /*
    * 新增菜品分类
    * */
    @PostMapping
    @ApiOperation(value = "新增菜品分类")
    public Result<String> save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增菜品分类:{}",categoryDTO);
        categoryService.saveCategory(categoryDTO);
        return Result.success();
    }
    /*
    * 启用、禁用分类
    * */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "启用、禁用分类")
    public Result<String> startOrstop(@PathVariable Integer status,Long id){
        log.info("修改分类状态");
        categoryService.startOrstop(status,id);
        return Result.success();
    }
    /**
     * 删除分类
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result<String> deleteById(Long id){
        log.info("删除分类：{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }
    /**
     * 修改分类
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result<String> update(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateWithcategory(categoryDTO);
        return Result.success();
    }
    /*
    * 查询分类
    * */
    @GetMapping("/list")
    @ApiOperation(value = "查询分类")
    public Result<List<Category>> list(Category category){
        List<Category> categoryList = categoryService.listWithType(category);
        return Result.success(categoryList);
    }
}
